
#import "PercentCurrentViewController.h"

#import "ColorUtility.h"
#import "CustomerDetailTableViewController.h"



@implementation PercentCurrentViewController

@synthesize detailItem = _detailItem;
@synthesize pageControl;
@synthesize scrollView;
@synthesize lineHostingView = _lineHostingView;
@synthesize sliderHostingView = _sliderHostingView;

@synthesize priorYearButton = _priorYearButton;
@synthesize percent91DaysDelinquentButton = _percent91DaysDelinquentButton;
@synthesize parameterButton=_parameterButton;

@synthesize pieHostingView = _pieHostingView;

@synthesize buttonsView = _buttonsView;
@synthesize notesButton = _notesButton;
@synthesize detailsButton = _detailsButton;

@synthesize masterPopoverController;
@synthesize dataModel=_dataModel;

#define kNumberOfPages 2


- (void) setDetailsButton:(UIButton *)detailsButton
{
    _detailsButton = detailsButton;
	
	[self setButtonStlye:_detailsButton];
    
}

- (void) setNotesButton:(UIButton *)notesButton
{
    _notesButton = notesButton;
	[self setButtonStlye:_notesButton];
    
}

- (void)setPriorYearButton:(UIButton *)priorYearButton
{   
    [self setButtonStlye:priorYearButton];
    
    _priorYearButton = priorYearButton;
    
}
- (void) setButtonsView:(UIView *)buttonsView
{
    _buttonsView = buttonsView;
    
    CAGradientLayer *gradientLayer = [ColorUtility toolBarGradientLayer];
    
    gradientLayer.frame = _buttonsView.layer.bounds;
    [_buttonsView.layer insertSublayer:gradientLayer atIndex:0];
}
- (IBAction)priorPeriodYearButtonPressed:(id)sender {
    
    if (isShowpriorYear){
        [linePlot removePlot: priorYearIdentifier];
        isShowpriorYear = NO;
        self.priorYearButton.titleLabel.textColor = [UIColor whiteColor];
    }
    else{
        [linePlot addPlot:priorYearIdentifier];
        isShowpriorYear = YES;
        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
            self.priorYearButton.highlighted = YES;
            self.priorYearButton.titleLabel.textColor = [UIColor blueColor];
        }];
        
    }
}
- (void)setParameterButton:(UIButton *)parameterButton
{   
   
    _parameterButton = parameterButton;
    
}

- (IBAction)percent91DaysDelinquentButtonPressed:(id)sender {
    
    if (isShowPercent91DaysDelinquent){
        [linePlot removePlot: percentOver91DaysDelinquentIdentifier];
        isShowPercent91DaysDelinquent = NO;
        self.percent91DaysDelinquentButton.titleLabel.textColor = [UIColor whiteColor];
    }
    else{
        [linePlot addPlot:percentOver91DaysDelinquentIdentifier];
        isShowPercent91DaysDelinquent = YES;
        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
            self.percent91DaysDelinquentButton.highlighted = YES;
            self.percent91DaysDelinquentButton.titleLabel.textColor = [UIColor blueColor];
        }];
        
    }
}

-(void) setPercent91DaysDelinquentButton:(UIButton *)percent91DaysDelinquentButton
{
   [self setButtonStlye:percent91DaysDelinquentButton];
    _percent91DaysDelinquentButton=percent91DaysDelinquentButton;
}

- (void)setButtonStlye:(UIButton *)button
{   
    CAGradientLayer *gradientLayer = [ColorUtility buttonGradientLayer];
    gradientLayer.frame = button.layer.bounds;
    [button.layer insertSublayer:gradientLayer atIndex:0];	
    button.titleLabel.textColor = [UIColor whiteColor];        
}

- (void)setDetailItem:(id)newDetailItem
{
    if (_detailItem != newDetailItem) {
        _detailItem = newDetailItem;
        
        // Update the view.
        [self configureView];
    }
    
    if (self.masterPopoverController != nil) {
        [self.masterPopoverController dismissPopoverAnimated:YES];
    }        
}

- (void)configureView
{
    // Update the user interface for 
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * kNumberOfPages, self.scrollView.frame.size.height);
    
    self.scrollView.delegate = self;
    self.view.backgroundColor = [UIColor colorWithWhite:0.7f alpha:1.0f];
    
    
     _dataModel = [[PercentCurrentDataModel alloc] init];
    

        
    
    EskPlotTheme *defaultTheme = [[EskPlotTheme alloc] init];
    
    //draw the pie plot
    NSArray *data= [_dataModel.metricsItems allValues];
    
    MetricsItem *item=[data objectAtIndex:[data count]-1];
    
    piePlot=[[PercentCurrentPiePlot alloc]initWithMetricsData:item];
    
    [piePlot renderInLayer:_pieHostingView withTheme:nil]; 
    
    
    //draw the line plots    
    linePlot = [[PercentCurrentLinePlot alloc] initWithMetricsData: _dataModel.metricsItems];
    
    linePlot.delegate=piePlot;
    
    [linePlot renderInLayer:_lineHostingView withTheme:defaultTheme];  
    
    
    //draw the slider plot
    sliderPlot = [[PercentCurrentSliderPlot alloc] initWithMetricsData:_dataModel.metricsItems 
                                                           monthlyData: _dataModel.monthlyKeys];
    sliderPlot.delegate = self;
    
    [sliderPlot renderInLayer:_sliderHostingView withTheme:defaultTheme];    

   
       
}




- (void)scrollViewDidScroll:(UIScrollView *)sender {
    // Update the page when more than 50% of the previous/next page is visible
    CGFloat pageWidth = self.scrollView.frame.size.width;
    int page = floor((self.scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
    self.pageControl.currentPage = page;
    
}


- (IBAction)changePage:(UIPageControl *)sender{
    // update the scroll view to the appropriate page  
    CGRect frame;
    frame.origin.x = self.scrollView.frame.size.width * self.pageControl.currentPage;
    frame.origin.y = 0;
    frame.size = self.scrollView.frame.size;
    [self.scrollView scrollRectToVisible:frame animated:YES];
}



- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    isPageControlBeingUsed = NO;
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    isPageControlBeingUsed = NO;
}


- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    //NSLog(@"segue=%@",segue.identifier);
    
    if ([segue.identifier isEqualToString:@"PercentCurrentToCollectorSegue"])
    {
        CustomerDetailTableViewController *collectorVC = (CustomerDetailTableViewController *)segue.destinationViewController;
        collectorVC.objectType = @"Collector";
    } 
}


- (void)viewDidUnload
{
    [self setPageControl:nil];
    [self setScrollView:nil];
    [self setLineHostingView:nil];
    [self setSliderHostingView:nil];   
    [self setPieHostingView:nil];    
    
    [self setPriorYearButton:nil];
    [self setPercent91DaysDelinquentButton:nil];
    [self setButtonsView:nil];
    [self setNotesButton:nil];
    
    [super viewDidUnload];
    
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    
	return YES;
}

- (void)splitViewController:(UISplitViewController *)splitController willHideViewController:(UIViewController *)viewController withBarButtonItem:(UIBarButtonItem *)barButtonItem
       forPopoverController:(UIPopoverController *)popoverController
{
    barButtonItem.title = NSLocalizedString(@"Master", @"Master");
    [self.navigationItem setLeftBarButtonItem:barButtonItem animated:YES];
    self.masterPopoverController = popoverController;
}

- (void)splitViewController:(UISplitViewController *)splitController willShowViewController:(UIViewController *)viewController invalidatingBarButtonItem:(UIBarButtonItem *)barButtonItem
{
    // Called when the view is shown again in the split view, invalidating the button and popover controller.
    [self.navigationItem setLeftBarButtonItem:nil animated:YES];
    self.masterPopoverController = nil;
}

#pragma mark - PercentCurrentsliderPlot Delegate

- (void)sliderPlot:(PercentCurrentSliderPlot  *)plot fromDate:(NSString *)fromDateString toDate:(NSString *)toDateString{
    
    [linePlot respondToRangeChangeFromDate:fromDateString toDate:toDateString];
    
}

@end
